/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.worldscale.xp.wslib.im.jabber.impl;

import net.worldscale.xp.wslib.im.jabber.JabberClient;

/**
 *
 * @author Kun
 */
final class ClientSession {

    private UserSession mCurUser;
    private final JabberEventRouter mEventRouter;
    private boolean mIsClosed = false;
    private JabberClient.LoginContext mLC;

    public ClientSession() {
        mEventRouter = new JabberEventRouter();
    }

    public void proc() {
        System.out.println(this + ".proc - begin");
        try {
            for (; !this.mIsClosed;) {
                final JabberClient.LoginContext lc = mLC;
                if (lc != null) {
                    mLC = null;
                    final UserSession us = new UserSession(this, lc);
                    this.mCurUser = us;
                    us.proc();
                    this.mCurUser = null;
                } else {
                    Thread.sleep(500);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(this + ".proc - end");
    }

    public void login(JabberClient.LoginContext lc) {
        this.mLC = lc;
    }

    public void close() {
        mIsClosed = true;
    }

    public UserSession getCurUser() {
        return mCurUser;
    }

    public JabberEventRouter getEventRouter() {
        return mEventRouter;
    }

    public int getPhase() {
        final UserSession us = this.mCurUser;
        if (us == null) {
            return JabberClient.PHASE_UNLOGIN;
        } else {
            return us.getPhase();
        }
    }
}
